package com.itheima;
//private static final long serialVersionUID = 1086053664494604050L;
import java.io.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Demo_IO_String {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /*String s = "黑马程序员";
        byte[] bytes = s.getBytes();
        System.out.println(Arrays.toString(bytes));
        System.out.println("-------------------------------------------------------------------------------");
        byte[] bytes1 = s.getBytes("GBK");
        System.out.println(Arrays.toString(bytes1));
        byte[] bytes3 = {-23, -69, -111, -23, -87, -84, -25, -88, -117
                , -27, -70, -113, -27, -111, -104};
        String s3 = new String(bytes3);
        System.out.println(s3);
        //-------------------------------------------------------------------------------
        byte[] bytes4={-70, -38, -62, -19, -77, -52, -48, -14, -44, -79};
        String s4 = new String(bytes4,"gbk");
        System.out.println(s4);*/
        //写入FileWriter
        /*FileWriter fw = new FileWriter("d13_IO_String/a.txt");
        //写一个字符
        fw.write('a');
        fw.write("\r\n");
        //写一个字符串
        fw.write("黑马程序员");
        fw.write("\r\n");
        //写一个字符数组
        char[] chars = {'I', ' ', 'a', 'm', ' ', 'k', 'i', 'n', 'g', '!', '!', '!'};
        fw.write(chars);
        fw.write("\r\n");
        //写字符串的一部分
        fw.write("黑马程序员", 0, 2);
        fw.write("\r\n");
        //fw.close();
        fw.flush();
        fw.write("good good study");
        fw.write("\r\n");
        fw.write("day day up");
        fw.close();*/
        //读取FileReader
        /*FileReader fr = new FileReader("d13_IO_String/a.txt");
        //int ch;
        while ((ch=fr.read())!=-1){
            System.out.print((char) ch);
        }
        System.out.println("----------------------------------");
        char[] chars = new char[1024];
        int len;
        while ((len=fr.read(chars))!=-1){
            System.out.println(new String(chars,0,2));
        }
        fr.close();*/
        //字符缓冲输出流
        /*BufferedWriter bw = new BufferedWriter(new FileWriter("d13_IO_String/a.txt"));
        bw.write(97);
        bw.newLine();
        char[] chars = {97,98,99,100};
        bw.write(chars);
        bw.newLine();
        bw.write("good good study");
        bw.newLine();
        bw.write("day day up");
        bw.close();*/
        //readLine
        /*BufferedReader br = new BufferedReader(new FileReader("d13_IO_String/a.txt"));
        String s = br.readLine();
        String s1 = br.readLine();
        String s2 = br.readLine();
        String s3 = br.readLine();
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        br.close();*/
        //排序
        /*BufferedReader br = new BufferedReader(new FileReader("d13_IO_String/a.txt"));
        String line = br.readLine();
        br.close();
        String[] s = line.split(" ");
        int[] arr = new int[s.length];
        for (int i = 0; i < arr.length; i++) {
            int num = Integer.parseInt(s[i]);
            arr[i] = num;
        }
        Arrays.sort(arr);
        BufferedWriter bw = new BufferedWriter(new FileWriter("d13_IO_String/a.txt"));
        for (int i = 0; i < arr.length; i++) {
            bw.write(arr[i]+" ");
            bw.newLine();
            bw.flush();
        }
        bw.close();*/
        //转换流
        //文件--------------------------------------->----内存---->-------------------------------------文件
        //---->字节流-->InputSteamReader-->FileReader<------------>FileWrite-->OutputWrite-->字节流---->----
        //
        /*ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d13_IO_String/oos.txt"));
        User u1 = new User("admin1", "123456");
        User u2 = new User("admin2", "654321");
        User u3 = new User("admin3", "456123");
        oos.writeObject(u1);
        oos.writeObject(u2);
        oos.writeObject(u3);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d13_IO_String\\oos.txt"));
        while (true) {
            try {
                User user = (User) ois.readObject();
                System.out.println(user);
            } catch (IOException e) {
                break;
            }
        }*/
        //Properties
        //创建集合对象
        /*Properties prop = new Properties();
        prop.put("zhangsan","23");
        prop.put("lisi","24");
        prop.put("wangwu","25");
        Set<Object> set = prop.keySet();
        for (Object key : set) {
            Object value = prop.get(key);
            System.out.println(key+"=="+value);
        }
        System.out.println("========================");
        Set<Map.Entry<Object, Object>> entrySet = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entrySet) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key+"=="+value);
        }
        System.out.println("========================");
        prop.forEach((key,value)-> System.out.println(key+"=="+value));
        System.out.println("========================");
        Set<String> stringSet = prop.stringPropertyNames();
        for (String key : stringSet) {
            String value = prop.getProperty(key);
            System.out.println(key+"=="+value);
        }*/
        //method1();
        method2();
    }

    private static void method2() throws IOException {
        //创建集合对象
        Properties prop = new Properties();
        //输入流
        FileReader fr =
                new FileReader(
                        "d13_IO_String/prop.properties");
        //调用load方法
        prop.load(fr);
        //释放资源
        fr.close();
        //遍历集合查看效果--把其中有效的配置信息以键值对的形式加载到集合中
        prop.forEach((k, v) -> System.out.println(k + "=" + v));
    }

    private static void method1() throws IOException {
        //创建集合对象
        Properties prop = new Properties();
        //添加数据
        prop.setProperty("001", "zhangsan");
        prop.setProperty("002", "lisi");
        prop.setProperty("003", "wangwu");
        //字节流指向文件
        FileOutputStream fos =
                new FileOutputStream(
                        "d13_IO_String/prop.properties");
        //写到流中
        prop.store(fos, "id&name");
        //释放资源
        fos.close();
    }
}
//User类
/*
class User implements Serializable {
    private String name;
    private transient String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}*/
